package net.zjitc.utils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created by dell on 2017/12/2.
 */
public class MailUtils {
    public static void sendMail(String code,String ToGO) throws Exception {
        //发送邮件
        /**
         * 1. 建立一个会话session
         * 2. 创建一个一代表邮件对象的Message
         * 3. 发送
         */
        //1. 建立一个会话session
        Properties prop = new Properties();
        //163.邮箱服务器
        prop.setProperty("mail.host","smtp.163.com");
        //授权
        prop.setProperty("mail.smtp.auth","true");
        Authenticator authenticator=new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("a497691172@163.com","xtl497691172");//qq的话需要开启服务，以及授权码
            }
        };
        Session session = Session.getInstance(prop, authenticator);

        //创建一个一代表邮件对象的Message
        Message message = new MimeMessage(session);
        //设置内容
        //发送给谁(谁发)
        message.setFrom(new InternetAddress("a497691172@163.com"));

        //发谁
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(ToGO));
        //设置标题
        message.setSubject("注册邮件");
        //设置内容(格式--html)\
        String content = "<a href=\"http://localhost:8080/shop/activeCode?code=" + code + "\">http://localhost:8080/shop/activeCode?code=" + code + "</a>";
        message.setContent(content, "text/html;charset=utf-8");

        //发送
        Transport.send(message);
    }
}
